//
//  LuckyMoneyVocherController.m
//  meiyinxiang
//
//  Created by apple on 2017/11/9.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "LuckyMoneyVocherController.h"
#import "VocherCell.h"
#import "VocherViewModel.h"
@interface LuckyMoneyVocherController ()
<UITableViewDataSource,
UITableViewDelegate>{
    VocherListViewModel *vocherListViewModel;
}
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation LuckyMoneyVocherController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configTableView];
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
   // self.navigationController.navigationBarHidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - PrivateMethod
- (void)loadData{
    if (vocherListViewModel == nil) {
        vocherListViewModel = [[VocherListViewModel alloc] init];
    }
    [vocherListViewModel loadDataWithCompletion:^(BOOL isSucess) {
        if (isSucess) {
            [self.tableView reloadData];
        }
    }];
}
- (void)configTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaBottomHeight-SafeAreaTopHeight) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"VocherCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:VocherCellReuseId];
   // [self.tableView reloadData];
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return vocherListViewModel.vocherModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VocherCell *vocherCell = [tableView dequeueReusableCellWithIdentifier:VocherCellReuseId forIndexPath:indexPath];
    if (vocherCell == nil) {
        vocherCell = [VocherCell loadNibWithVocherCell];
    }
    VocherViewModel *viewmodel = vocherListViewModel.vocherModels[indexPath.row];
    vocherCell.indexPath = indexPath;
    vocherCell.vocherModel = viewmodel;
    return vocherCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VocherViewModel *viewmodel = vocherListViewModel.vocherModels[indexPath.row];
    if (self.selectBlock) {
        self.selectBlock(viewmodel);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 155;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
